<?php
class Search extends Controller
{
	public $userLog;
	//Collection users.
	public $users;

    public function __construct() {
        parent::__construct();
        $this->_initalize();
    }

    public function index()
    {
    	$this->user->id = $this->userLog->id;

    	if(isset($this->_get['search']))
    	{
			$search = $this->_get['search'];
			$this->users = $this->user->getFavoriteUser($search['name'], $search['sex']);
    	}
    	else {
    		$this->users = $this->user->getFavoriteUser();
    	}

        $this->template('search');
    }

    private function _initalize($params = null) {
    	if (isset($this->_session['user'])) {
    		$this->userLog = $this->_session['user']['info'];

    	} else {
    		header('Location:index.php?c=login');
    	}

    	$this->model('user');
    	$this->library('timeunix');
    }

    public function addtofavorite()
    {
		if(isset($this->_post['favorite'])){
			$id = $this->_post['favorite']['id'];
			$result = $this->user->addFavorite($this->userLog->id, $id);

			if((bool)$result){
				echo "{result:1, id:{$id}}";
			}
			else {
				echo "{result:0, id:{$id}}";
			}
		}
    }
}
?>
